﻿using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Timers;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Collections.ObjectModel;
using System.Collections.Generic;

namespace TaskManagerMVVM.DataAccess
{
    public class SystemPerformance
    {


        private PerformanceCounter _CPUCounterValue = new PerformanceCounter("Processor", "% Processor Time", "_Total");
        private PerformanceCounter _MemCounterValue = new PerformanceCounter("Process", "Working Set", "_Total");

        private double CPUUsage;
        private double MemUsage;



        public SystemPerformance()
        {

            Timer timer = new Timer(1000);
            timer.Elapsed += new ElapsedEventHandler(timer_elapsed);
            timer.Enabled = true;
            timer.Start();

        }

        public static SystemPerformance CreateNewSystemPerformance()
        {
            return new SystemPerformance();
        }

        public double CPUCounter
        {
            get { return this.CPUUsage; }
            set
            {

                if (value != this.CPUUsage)
                {
                    this.CPUUsage = value;

                }
            }
        }

        public double MemCounter
        {
            get { return this.MemUsage; }
            set
            {
                if (value != this.MemUsage)
                {
                    this.MemUsage = value;

                }
            }
        }

        public void MeasureCPUUsage()
        {
            this.CPUUsage = this._CPUCounterValue.NextValue();
        }

        public void MeasureMemUsage()
        {
            this.MemUsage = this._MemCounterValue.NextValue();
        }


        private void timer_elapsed(object sender, ElapsedEventArgs args)
        {
            MeasureCPUUsage();
            MeasureMemUsage();
        }



        public List<double> GetMemUsage()
        {

            List<double> mem = new List<double>();
            mem.Add(MemUsage);
            return mem;
        }

        public List<double> GetCPUUsage()
        {
            List<double> cpuUsage = new List<double>();
            cpuUsage.Add(CPUUsage);
            return cpuUsage;
        }



    }
}
